<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\FinishTask;
use app\admin\model\Task;
use app\admin\model\UserRecord;
use app\admin\model\GameRecord;
use app\admin\model\AdLog;
use think\Db;


class Usertask extends Api {
    protected $noNeedLogin = ['receive'];
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /*
     *领取任务
     *
     */
    public function receive()
    {
        $this->checkParam(['sku']);
        $uid = $this->auth->id;
        $sku = $this->request->param('sku');
        $user = User::where('id', $uid)->find();
        $task = Task::where('sku', $sku)->find();
        if (!$task) {
            $this->error(__('No task'));
        }
        $finish = FinishTask::where('user_id', $uid)->where('task_sku', $sku)->whereTime('addtime', 'today')->find();

        if ($finish) {
            $this->error(__('The task was completed today'));
        }

        switch($task['task_type']) {
            case 0:
                $count = GameRecord::where('user_id', $uid)->where('game_id', $task['game_id'])->whereTime('addtime', 'today')->count();
                break;
            case 1:
                $count = AdLog::where('user_id', $uid)->whereTime('addtime', 'today')->count();
                break;
            case 2:
                $count = UserRecord::where('user_id', $uid)->where('type', 0)->where('change_type',0)->whereTime('addtime', 'today')->sum('num');
                break;
            case 3:
                $count = UserRecord::where('user_id', $uid)->where('type', 1)->where('change_type',0)->whereTime('addtime', 'today')->sum('num');
                break;
            case 4:
                $count = UserRecord::where('user_id', $uid)->where('type', 2)->where('change_type',0)->whereTime('addtime', 'today')->sum('num');
                break;
        }
        if ($count < $task['num']) {
            $this->error(__('Task not up to standard'));
        }

        $type = [
            0 => 'dollar',
            1 => 'coin',
            2 => 'card'
        ];
        $gift_type = $type[$task['gift_type']];
        Db::startTrans();
        $res = $this->updateUserAccount($uid, 2, 0, [$gift_type => $task['gift_num']]);
        if ($res['status']) {
            FinishTask::insert([
                'user_id' => $uid,
                'app_id' => $user['app_id'],
                'task_sku' => $sku,
                'addtime' => date('Y-m-d H:i:s', time())
            ]);
            Db::commit();
            $this->success('Successfully received', $user = User::where('id', $uid)->find());
        } else {
            Db::rollback();
            $this->error(__('Collection failed. Please try again later'));
        }


    }

    //修改账户记录
    protected function updateUserAccount($uid, $action, $type, $array, $ext=null) {
        $change = $type == 0 ? '+' : '-';
        $change_type = $type == 0 ? 0 : 1;
        $user = User::where('id', $uid)->lock(true)->find();
        if (!$user) {
            return array('status' => false, 'msg' => 'Account error');
        }
        $msg = [
            'coin' => 'Insufficient gold coins',
            'dollar' => 'The dollar is not enough',
            'card' => 'Insufficient withdrawal card'
        ];
        $type = [
            'dollar' => 0,
            'coin' => 1,
            'card' => 2
        ];
        $data = [
            'user_id' => $uid,
            'app_id' => $user['app_id'],
            'action' => $action,
            'change_type' => $change_type,
            'addtime' => date('Y-m-d H:i:s',time()),
            'ext' => $ext
        ];

        foreach ($array as $k => $v) {
            if ($v) {
                $data['type'] = $type[$k];
                if ($change == '-' && $v > $user[$k]) {
                    return array('status' => false, 'msg' => $msg[$k]);
                }
                $data['change_before'] = $user[$k];
                $data['change'] = $change . $v;
                $data['num'] = $v;
                $data['change_after'] = $user[$k] + $data['change'];
                User::where('id',$uid)->update([$k => $data['change_after']]);
                UserRecord::insert($data);
            }
        }
        return array('status' => true);
    }
}